import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '略略略',
    avatar: '',
    addmoredatax:[
    [
        {
        //   RectFeature:{
              id:"label-marker-id-1630399826257",
              props:{
                deleteMarkerId:"label-marker-id-1630399826257",
                name: "矢量图形",
                text: "黄金糕",
                textId: "label-text-id-1630399826257",
              },
              shape:{
                height: 188.2903981264637,
                width: 245.43325526932088,
                x: 142.0725995316159,
                y: 121.82669789227165,
              },
              style:{
                fillStyle: "rgba(255, 0, 0, 0)",
                lineWidth: 1,
                opacity: 1,
                strokeStyle: "#000",
              },
              type: "RECT"
        //   }
        },
        {
            // PolygonFeature:{
            id:"label-marker-id-1630400229749",
            props:{
                deleteMarkerId: "label-marker-id-1630400229749",
                name: "矢量图形2",
                text: "黄金糕",
                textId: "label-text-id-1630400229749",
              },
              shape:{
                points:[
                    {x: 617.0140515222482,y: 111.52224824355972},
                    {x: 668.536299765808,y: 107.77517564402808},
                    {x: 667.5995316159251,y: 217.37704918032787},
                    {x: 703.1967213114754,y: 386.93208430913353},
                    {x: 725.6791569086652,y: 497.4707259953162},
                    {x: 574.8594847775175,y: 492.78688524590166},
                    {x: 630.1288056206089,y: 356.95550351288057},
                    {x: 632.9391100702576,y: 276.39344262295083},
                    {x: 569.2388758782201,y: 263.27868852459017},
                    {x: 618.8875878220141,y: 199.5784543325527},
                ]
              },
              style:{
                fill: true,
                fillStyle: "#0f0",
                globalAlpha: 0.3,
                lineWidth: 1,
                opacity: 1,
                stroke: true,
                strokeStyle: "#00f",
              },
              type: "POLYGON"
        //   }
        },
      ],[
        {
        //   RectFeature:{
              id:"label-marker-id-1630399826257",
              props:{
                deleteMarkerId:"label-marker-id-1630399826257",
                name: "矢量图形",
                text: "黄金糕",
                textId: "label-text-id-1630399826257",
              },
              shape:{
                height: 188.2903981264637,
                width: 245.43325526932088,
                x: 142.0725995316159,
                y: 121.82669789227165,
              },
              style:{
                fillStyle: "rgba(255, 0, 0, 0)",
                lineWidth: 1,
                opacity: 1,
                strokeStyle: "#000",
              },
              type: "RECT"
        //   }
        },
        {
            // PolygonFeature:{
            id:"label-marker-id-1630400229749",
            props:{
                deleteMarkerId: "label-marker-id-1630400229749",
                name: "矢量图形2",
                text: "黄金糕",
                textId: "label-text-id-1630400229749",
              },
              shape:{
                points:[
                    {x: 617.0140515222482,y: 111.52224824355972},
                    {x: 668.536299765808,y: 107.77517564402808},
                    {x: 667.5995316159251,y: 217.37704918032787},
                    {x: 703.1967213114754,y: 386.93208430913353},
                    {x: 725.6791569086652,y: 497.4707259953162},
                    {x: 574.8594847775175,y: 492.78688524590166},
                    {x: 630.1288056206089,y: 356.95550351288057},
                    {x: 632.9391100702576,y: 276.39344262295083},
                    {x: 569.2388758782201,y: 263.27868852459017},
                    {x: 618.8875878220141,y: 199.5784543325527},
                ]
              },
              style:{
                fill: true,
                fillStyle: "#0f0",
                globalAlpha: 0.3,
                lineWidth: 1,
                opacity: 1,
                stroke: true,
                strokeStyle: "#00f",
              },
              type: "POLYGON"
        //   }
        },
      ],[
        {
        //   RectFeature:{
              id:"label-marker-id-1630399826257",
              props:{
                deleteMarkerId:"label-marker-id-1630399826257",
                name: "矢量图形",
                text: "黄金糕",
                textId: "label-text-id-1630399826257",
              },
              shape:{
                height: 188.2903981264637,
                width: 245.43325526932088,
                x: 142.0725995316159,
                y: 121.82669789227165,
              },
              style:{
                fillStyle: "rgba(255, 0, 0, 0)",
                lineWidth: 1,
                opacity: 1,
                strokeStyle: "#000",
              },
              type: "RECT"
        //   }
        },
        {
            // PolygonFeature:{
            id:"label-marker-id-1630400229749",
            props:{
                deleteMarkerId: "label-marker-id-1630400229749",
                name: "矢量图形2",
                text: "黄金糕",
                textId: "label-text-id-1630400229749",
              },
              shape:{
                points:[
                    {x: 617.0140515222482,y: 111.52224824355972},
                    {x: 668.536299765808,y: 107.77517564402808},
                    {x: 667.5995316159251,y: 217.37704918032787},
                    {x: 703.1967213114754,y: 386.93208430913353},
                    {x: 725.6791569086652,y: 497.4707259953162},
                    {x: 574.8594847775175,y: 492.78688524590166},
                    {x: 630.1288056206089,y: 356.95550351288057},
                    {x: 632.9391100702576,y: 276.39344262295083},
                    {x: 569.2388758782201,y: 263.27868852459017},
                    {x: 618.8875878220141,y: 199.5784543325527},
                ]
              },
              style:{
                fill: true,
                fillStyle: "#0f0",
                globalAlpha: 0.3,
                lineWidth: 1,
                opacity: 1,
                stroke: true,
                strokeStyle: "#00f",
              },
              type: "POLYGON"
        //   }
        },
      ],[
        {
        //   RectFeature:{
              id:"label-marker-id-1630399826257",
              props:{
                deleteMarkerId:"label-marker-id-1630399826257",
                name: "矢量图形",
                text: "黄金糕",
                textId: "label-text-id-1630399826257",
              },
              shape:{
                height: 188.2903981264637,
                width: 245.43325526932088,
                x: 142.0725995316159,
                y: 121.82669789227165,
              },
              style:{
                fillStyle: "rgba(255, 0, 0, 0)",
                lineWidth: 1,
                opacity: 1,
                strokeStyle: "#000",
              },
              type: "RECT"
        //   }
        },
        {
            // PolygonFeature:{
            id:"label-marker-id-1630400229749",
            props:{
                deleteMarkerId: "label-marker-id-1630400229749",
                name: "矢量图形2",
                text: "黄金糕",
                textId: "label-text-id-1630400229749",
              },
              shape:{
                points:[
                    {x: 617.0140515222482,y: 111.52224824355972},
                    {x: 668.536299765808,y: 107.77517564402808},
                    {x: 667.5995316159251,y: 217.37704918032787},
                    {x: 703.1967213114754,y: 386.93208430913353},
                    {x: 725.6791569086652,y: 497.4707259953162},
                    {x: 574.8594847775175,y: 492.78688524590166},
                    {x: 630.1288056206089,y: 356.95550351288057},
                    {x: 632.9391100702576,y: 276.39344262295083},
                    {x: 569.2388758782201,y: 263.27868852459017},
                    {x: 618.8875878220141,y: 199.5784543325527},
                ]
              },
              style:{
                fill: true,
                fillStyle: "#0f0",
                globalAlpha: 0.3,
                lineWidth: 1,
                opacity: 1,
                stroke: true,
                strokeStyle: "#00f",
              },
              type: "POLYGON"
        //   }
        },
    ]]
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

